<?php
function PHPtoOrgChart(array $arr, $title = '', $level_name = array('1' => '业务员', '2' => '业务主任', '3' => '业务经理', '4' => '业务高管'), $level_color = array('1' => 'green', '2' => 'blue', '3' => 'red', '4' => 'yellow'))
{
    echo '<table>';
    $div_style = '';
    $size = count($arr);
    if ($title != '') {
        //head
        $title_level = explode('/', $title);
        if (isset($_GET['id']) && $_GET['id'] == $title_level[0]) {
            $div_style = "border: 3px solid red;";
        } else {
            $div_style = '';
        }

        $title_tmp = $title_level[1];
        if (isset($title_level[2]) && $title_level[2] > 0) {

            $title_tmp .= '/' . '<font color="' . $level_color[$title_level[2]] . '">' . $level_name[$title_level[2]] . '</font>';
        }
        echo '<tr>';
        echo '<td colspan="' . ($size * 2) . '">';
        echo '<div style="' . $div_style . '"  class="charttext">' . $title_tmp . '</div>';
        echo '</td>';
        echo '</tr>';
        //head line


        echo '<tr>';
        echo '<td colspan="' . ($size * 2) . '">';
        echo '<table><tr><th class="right width-50"></th><th class="width-50"></th></tr></table>';
        echo '</td>';
        echo '</tr>';

        //line
        if ($size >= 2) {

            $tdWidth = ((100) / ($size * 2));

            echo '<tr>';
            echo '<th class="right" width="' . $tdWidth . '%"></th>';
            echo '<th class="top" width="' . $tdWidth . '%"></th>';
            for ($j = 1; $j < $size - 1; $j++) {
                echo '<th class="right top" width="' . $tdWidth . '%"></th>';
                echo '<th class=" top" width="' . $tdWidth . '%"></th>';
            }
            echo '<th class="right top" width="' . $tdWidth . '%"></th>';
            echo '<th width="' . $tdWidth . '%"></th>';
            echo '</tr>';
        }
    }
    //
    echo '<tr>';
    foreach ($arr as $key => $value) {
        echo '<td  colspan="2">';
        if (is_array($value)) {

            PHPtoOrgChart($value, $key);
        } else {

            $title_level = explode('/', $value);
            if (isset($_GET['id']) && $_GET['id'] == $title_level[0]) {
                $div_style = "border: 3px solid red;";
            } else {
                $div_style = '';
            }
            $title_tmp = $title_level[1];
            if (isset($title_level[2]) && $title_level[2] > 0) {
                $title_tmp .= '/' . '<font color="' . $level_color[$title_level[2]] . '">' . $level_name[$title_level[2]] . '</font>';
            }
            echo '<div style="' . $div_style . '" class="charttext">' . $title_tmp . '</div>';
        }
        echo '</td>';
    }
    echo '</tr>';
    //
    echo '</table>';
}
